package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Tran;
import com.example.demo.mapper.TranMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class TranService {

    @Resource
    private TranMapper tranMapper;

    public List<Tran> getList(String number){
        List<Tran> list = tranMapper.getList(number);
        System.out.println(list);
        return list;
    }

    public int register(Tran tran){
        System.out.println("transportation: "+tran);
        Tran info = tranMapper.selectByNumber(tran.getNum());
        if(info==null){
            return tranMapper.insert(tran);
        }else return -1;
    }

    public int update(Tran tran){
        System.out.println("SERVICE_transportation: "+tran);
        Tran info = tranMapper.selectByNumber(tran.getNum());
        tran.setId(info.getId());
        return tranMapper.updateById(tran);
    }

    public Integer delete(Integer id){
        return tranMapper.deleteById(id);
    }
    public Integer delete(Tran tran){
        QueryWrapper qw = new QueryWrapper<Tran>();
        qw.eq("num",tran.getNum());
        return tranMapper.delete(qw);
    }
}
